package com.lxm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lxm.dao.AdminRoleMapper;
import com.lxm.dao.RoleResourceMapper;
import com.lxm.entity.Resource;
import com.lxm.dao.ResourceMapper;
import com.lxm.entity.RoleResource;
import com.lxm.service.IResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author lxm
 * @since 2021-11-18
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements IResourceService {
    @javax.annotation.Resource
    private RoleResourceMapper rrm;
    //获取所有资源列表，权限树
    public List<Resource> showAllResource() {
        List<Resource> resources = baseMapper.selectList(null);
        List<Resource> list = resources.stream().filter((resource -> resource.getParentId() == 0)).peek(resource -> resource.setChildren(selectAllChildren(resource.getMenuId(), resources))).collect(Collectors.toList());
        return list;
    }
    //查询该目录下的所有子目录
    public List<Resource> selectAllChildren(int pid, List<Resource> list){
        List<Resource> collect = list.stream().filter(resource -> resource.getParentId() == pid).peek(resource -> resource.setChildren(selectAllChildren(resource.getMenuId(), list))).collect(Collectors.toList());
        return collect;
    }

    //根据角色ID查的该角色下的所有权限资源
    public List<Resource> selectAllResourceByRoleIds(List<Integer> rIds) {
        List<Resource> resources = baseMapper.selectAllResourceByRoleIds(rIds);
        List<Resource> list = resources.stream().filter((resource -> resource.getParentId() == 0)).peek(resource -> resource.setChildren(selectAllChildren(resource.getMenuId(), resources))).collect(Collectors.toList());
        return list;
    }
}
